package com.yanfei.zero.infrastructure.workflow;

import java.util.HashMap;
import java.util.Map;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.camunda.bpm.client.task.ExternalTaskService;
import org.springframework.stereotype.Component;

import com.yanfei.zero.domain.order.repository.OrderRepository;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@Component
@ExternalTaskSubscription("createOrder") // create a subscription for this topic name
@Slf4j
public class CamundaCreateOrderHandler implements ExternalTaskHandler {
	
	  @Resource
	  private OrderRepository orderRepository;

	  @Override
	  public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {

	    // only for the sake of this demonstration, we generate random data
	    // in a real-world scenario, we would load the data from a database
		String orderNumber =  externalTask.getBusinessKey();
		Map<String,Object> map = new HashMap<>();
		map.put("errorCode", "Order Automatically Canceled");
		  map.put("orderNumber", orderNumber);
		externalTaskService.setVariables(externalTask,map);
	    externalTaskService.complete(externalTask,map);
	    log.info("create order!",orderNumber);
	  }

	}
